package com.huanletao.dom4j;

import com.huanletao.dom4j.pojo.*;
import org.dom4j.DocumentException;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
/**
 * <p>
 *  @see DocumentResolver
 *  XmlDocumentResolver 使用示例, 主要实现将xml文档内容,填充到javabean对象中,
 *  实现的功能依赖 javax.xml.bind.annotation.XmlElement 注解实现
 * </p>
 *
 * package: com.huanletao.dom4j
 * Description:
 * className: XmlDocumentResolverExample
 * author: 踏雪无痕
 * copyright: Copyright (c) 2022/7/30 11:45
 * since: V1.0
 */
public class XmlDocumentResolverExample {

    /**
     * 解析简单文档
     */
    @Test
    public void resolveSimpleDocument() throws IOException, DocumentException {
        ClassPathResource resource = new ClassPathResource("configuration/SimpleXml.xml");
        Student student = DocumentResolver.resolve(resource.getInputStream(), Student.class);
        System.out.println(student);
    }

    /**
     * 解析泛型文档
     * @throws IOException
     */
    @Test
    public void resolveDocumentByGeneric() throws IOException, DocumentException {
        ClassPathResource resource = new ClassPathResource("configuration/myConfig.xml");
        Employee<Address> employee = DocumentResolver.resolve(resource.getFile(), Emp.class);
        System.out.println(employee.getBody());
    }

}
